<template>
	<view class="container">	
		<!--申请-->
		<uni-forms class="" ref="formData" :modelValue="formData" label-position="top" label-width="200">				
			<view class="m-b30 p-30 bg-white">
				<uni-forms-item label="工地评价">
					<comment :isclick="info ? false : true" type="star" :data="serviceScore" @result="commentResult"></comment>
					<view class="m-t20">
						<uni-easyinput type="textarea" autoHeight v-model="formData.content" placeholder="呼起软件盘写出更有帮助性的评价哦~" />
					</view>	
					<view class="m-t20">
						<view class="imgs uni-flex justify-left flex-auto-row" v-if="formData.images.length>0">
							<view v-for="(item,ikey) in formData.images" class="imgs_item1">
								<image class="bg-grayf5" :src="item" mode="aspectFill" @click="viewImage(formData.images, ikey)"></image>
								<uni-icons class="imgs_remove" type="closeempty" size="10" color="#ffffff" @click="imageRemove(ikey)" v-if="!info"></uni-icons>
							</view>
						</view>
						<view class="imgs_item1 bg-grayf5 uni-flex justify-center align-center flex-column text-center m-t20 m-b20" @click="upload" v-if="!info">
							<view><uni-icons type="plusempty" size="20"color="#999999"></uni-icons></view>
							<view class="fs12 text-gray9">添加图片</view>
						</view>
					</view>
				</uni-forms-item>
			</view>			
			<view class="m-b30 p-30 bg-white">
				<uni-forms-item label="服务人员评价">
					<comment :isclick="info ? false : true" type="number" :data="personScore" @result="commentResult"></comment>
					<view class="fs11 text-gray9 p-t10 p-b10">满意请给10分哦</view>
				</uni-forms-item>		
			</view>
		</uni-forms>
		<button class="round-40 bg-blue text-white fs14 m-t30 m-b30 m-b10" @click="submitForm" v-if="!info">提交</button>
	</view>
</template>

<script>
import { listBySubDh, unlistBySubDh, commentCreate } from '@/api/comment.js';
import urlConfig from '@/common/config.js';
import { uploadImage } from '@/common/js/upload.js'
export default {
	data() {
		return {
			Sub_dh: "",		//工地编码
			info: "",		//申请信息
			images: [],	
			formData: {	// 表单数据
				sub_dh: "",		//工地编码
				content: "",	//评价内容
				star: 5,		//评分
				images: "",		//评价图片
				person: [],		//服务人员评价
			},
			serviceScore: [ {"title": "", "star": 5} ],
			personScore: []
		}
	},
	onLoad(e) {
		this.Sub_dh = e.Sub_dh ? e.Sub_dh : ""
		this.getInfo();
	},
	methods: {
		/**
		 * 获取工地详情
		 */
		getInfo(){
			let that = this;
			listBySubDh({sub_dh: that.Sub_dh}).then((res) => {
				if(res.code == 0 && res.data && res.data.sub_dh != undefined){
					that.info = res.data
					that.$set(that.formData, "person", res.data.person)
					that.$set(that.formData, "content", res.data.content)
					that.$set(that.formData, "images", res.data.images)
					that.personFormat(res.data.person)
				}else {
					unlistBySubDh({sub_dh: that.Sub_dh}).then((res1) => {
						that.$set(that.formData, "person", res1.data.person)
						that.personFormat(res1.data.person)
					})
				}
			})			
		},
		/**
		 * 人员评分格式化
		 */
		personFormat(data){
			let person = []
			data.forEach(item => {
				person.push({
					title: item.person_type,
					star: item.star
				})
			})
			this.personScore = person
		},
		/**
		 * 提交开票申请
		 */
		submitForm() {
			let that = this
			let params = this.formData
			if(params.star <= 0){
				uni.showToast({ title: "请对工地评分", icon: 'none' })
				return;				
			}
			let person_name = ""
			params.person.forEach(item => {
				if(item.star <= 0) {
					person_name = item.person_liable_name
				}
			})
			if(person_name != "") {
				uni.showToast({ title: "请对所有服务人员评分", icon: 'none' })
				return;					
			}	
			params.sub_dh = that.Sub_dh
			commentCreate(params).then(res => {
				if(res.code == 0) {
					uni.showToast({ title: '提交成功', icon: 'none' })
					uni.navigateBack({
						delta: 1,
						success: () => {
							var pages = getCurrentPages();
							var prePage = pages[pages.length - 1] //上一个页面
							prePage.$vm.getList()
						}
					})
				}
			})
		},
		/**
		 * 评分
		 * @param {Object} e
		 */
		commentResult(e) {
			if(e.type == "star") {
				this.$set(this.formData, "star", e['data'][0]['star'])				
			}else if(e.type == "number") {
				let person = this.formData.person
				e.data.forEach((item,key) => {
					person[key]['star'] = item.star
				})
				this.$set(this.formData, "person", person)
			}
		},
		/**
		 * 上传图片
		 */
		upload(){
			let that = this
			uploadImage(9).then(res => {
				if(res) {					
					that.images = that.images.concat(res)
					that.$set(that.formData, "images", that.images)
				}
			})
		},
		/**
		 * 删除图片
		 */
		imageRemove(index){
			this.images.splice(index, 1);
			this.$set(this.formData, "images", this.images);
		},
		/**
		 * 查看大图
		 */
		viewImage(imgs,index){
			uni.previewImage({
				current: index,
				urls: imgs
			})
		},
	}
}
</script>

<style>
.container {
	min-height: 100vh;
	padding: 30rpx;
}
.exam_state {
	text-align: center;
}
.info_row {
    position: relative;
    display: flex;
    flex-direction: row;
	font-size: 28rpx;
	line-height: 72rpx;
	margin-bottom: 20rpx;
}
.info_row_label {
	color: #606266;
	width: 210rpx;
	padding-right: 24rpx;
}
.info_row_value {
	position: relative;
	flex: 1;
	box-sizing: border-box;
	flex-direction: row;
}
</style>